package com.zzh.lib.views;


import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;

import com.zzh.lib.views.banner.AttrsController;
import com.zzh.lib.views.banner.BaseIndicatorView;
import com.zzh.lib.views.banner.IndicatorOptions;
import com.zzh.lib.views.drawer.BaseDrawer;
import com.zzh.lib.views.drawer.DrawerProxy;
import com.zzh.lib.views.enums.IndicatorOrientation;

import androidx.annotation.Nullable;

/**
 * Created by ZZH on 2023/2/16.
 *
 * @date: 2023/2/16 08:55
 * @email: zzh_hz@126.com
 * @author: zzh
 * @description: com.zzh.lib.views
 */
public class IndicatorView extends BaseIndicatorView {
    private DrawerProxy mDrawerProxy;


    private void initPIV(AttributeSet attrs) {
        AttrsController.initAttrs(context, attrs, mIndicatorOptions);
        mDrawerProxy = new DrawerProxy(mIndicatorOptions);
    }

    public IndicatorView(Context context) {
        super(context);
        initPIV(null);
    }

    public IndicatorView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPIV(attrs);
    }

    public IndicatorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPIV(attrs);
    }

    public IndicatorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initPIV(attrs);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        mDrawerProxy.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        BaseDrawer.MeasureResult measureResult = mDrawerProxy.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(measureResult.getMeasureWidth(), measureResult.getMeasureHeight());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        rotateCanvas(canvas);
        mDrawerProxy.onDraw(canvas);
    }

    public void setIndicatorOptions(IndicatorOptions options) {
        super.setIndicatorOptions(options);
        mDrawerProxy.setIndicatorOptions(options);
    }

    public void notifyDataChanged() {
        mDrawerProxy = new DrawerProxy(mIndicatorOptions);
        super.notifyDataChanged();
    }

    public void rotateCanvas(Canvas canvas) {
        if (mIndicatorOptions.getOrientation() == IndicatorOrientation.INDICATOR_VERTICAL) {
            canvas.rotate(90f, getWidth() / 2f, getWidth() / 2f);
        } else if (mIndicatorOptions.getOrientation() == IndicatorOrientation.INDICATOR_RTL) {
            canvas.rotate(180f, getHeight() / 2f, getHeight() / 2f);
        }
    }

    public void setOrientation(int orientation) {
        mIndicatorOptions.setOrientation(orientation);
    }
}
